package vendalenger.debvi;

import java.awt.Component;
import java.awt.Desktop;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import javafx.application.Application;
import javafx.stage.Stage;
import vendalenger.debvi.editors.Editor;
import vendalenger.debvi.editors.Welcome_Screen;
import vendalenger.debvi.jfx.DebviFrame;
import vendalenger.port.Command;

public class Debvi extends Application {

	protected static String debviVersion = "v0.0.8 Acacia";
	public static ArrayList<Editor> toBeOpened;
	public static DProject openedProject;
	public static File selectedFile;

	/**
	 * Return the current version of Debvi.
	 * 
	 * @return The version of Debvi
	 */
	public static String getDebviVersion() {
		return debviVersion;
	}

	/**
	 * Open a file using the default opener.
	 */
	public static void openFile(File f) {
		if (f.exists()) {
			if (f.isFile()) {
				boolean unsupported = true;
				for (int i = 0; i < Preferences.knownFileFormats.size(); i++) {
					if (f.getName().endsWith(
							Preferences.knownFileFormats.get(i))) {
						Command.issue(Preferences.knownFileFormatsCommand
								.get(i).replace("#", f.getAbsolutePath()));
						unsupported = false;
					}
					;
				}
				if (unsupported) {
					try {
						Desktop.getDesktop().open(f);
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		} else {
			System.err.println("File " + f.getName() + " does not exist.");
		}
	}

	/**
	 * Open a project and setup the Stage.
	 */
	public static void setup() {
		DebviFrame.tabTags = new ArrayList<String>();
		
		toBeOpened = new ArrayList<Editor>();

		Preferences.write();
		System.out.println("> To clear the console use ^clr.");
		System.out.println("> For help, use ^help.");
		launch(Main.args);
	}

	@Override
	public void start(Stage primaryStage) throws Exception {
		System.out.println("Launching javafx");
		DebviFrame.setup(primaryStage);
		new Welcome_Screen().open(new String[] {});
		Main.loadingScreen.dispose();
	}
}
